package com.DreamTeam.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.DreamTeam.dao.IOrderDetailDao;
import com.DreamTeam.po.OrderDetail;

public class OrderDetailDao implements IOrderDetailDao {
	private Connection conn = null;

	public OrderDetailDao(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean doAdd(OrderDetail orderDetail) {
		String sql = "insert into orderDetail values(?,?,?,?)";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, orderDetail.getOrderNo());
			prsmt.setString(2, orderDetail.getProductNo());
			prsmt.setInt(3, orderDetail.getQty());
			prsmt.setDouble(4, orderDetail.getOrderPrice());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	@Override
	public boolean doUpdate(OrderDetail orderDetail) {
		String sql = "update orderDetail set orderNo=?,productNo=?,Qty=?,OrderPrice=? where orderNo=? and productNo=?";
		int flag = 0;
		PreparedStatement prsmt = null;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, orderDetail.getOrderNo());
			prsmt.setString(2, orderDetail.getProductNo());
			prsmt.setInt(3, orderDetail.getQty());
			prsmt.setDouble(4, orderDetail.getOrderPrice());
			prsmt.setString(5, orderDetail.getOrderNo());
			prsmt.setString(6, orderDetail.getProductNo());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	@Override
	public boolean doDelete(OrderDetail orderDetail) {
		String sql = "delete from orderDetail where orderNo=? and productNo=?";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, orderDetail.getOrderNo());
			prsmt.setString(2, orderDetail.getProductNo());
			flag= prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	@Override
	public List<OrderDetail> queryAll() {
		String sql = "select * from orderDetail";
		PreparedStatement prsmt = null;
		List<OrderDetail> list =new ArrayList();
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderDetail od = new OrderDetail();
				od.setOrderNo(re.getString(1));
				od.setProductNo(re.getString(2));
				od.setQty(re.getInt(3));
				od.setOrderPrice(re.getDouble(4));
				list.add(od);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<OrderDetail> queryOrderByColumnName(String columnName,String model) {
		String sql = "select * from orderDetail order by "+columnName+" "+model;
		PreparedStatement prsmt = null;
		List<OrderDetail> list =new ArrayList();
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderDetail od = new OrderDetail();
				od.setOrderNo(re.getString(1));
				od.setProductNo(re.getString(2));
				od.setQty(re.getInt(3));
				od.setOrderPrice(re.getDouble(4));
				list.add(od);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<OrderDetail> queryOrderDetailByColumn(String keyword,
			String columnName, String orderColumn, String model) {
		String sql = "select * from orderDetail where "+columnName+" like ? order by "+orderColumn+" "+model;
		
		PreparedStatement prsmt = null;
		List<OrderDetail> list =new ArrayList();
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, "%"+keyword+"%");
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderDetail od = new OrderDetail();
				od.setOrderNo(re.getString(1));
				od.setProductNo(re.getString(2));
				od.setQty(re.getInt(3));
				od.setOrderPrice(re.getDouble(4));
				list.add(od);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<OrderDetail> queryByKeyword(String keyword) {
		String sql = "select * from orderDetail where orderNo like ? or productNo like ? " +
				"or qty like ? or orderPrice like ?";
		PreparedStatement prsmt=null;
		List<OrderDetail> list = new ArrayList();
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, "%"+keyword+"%");
			prsmt.setString(2, "%"+keyword+"%");
			prsmt.setString(3, "%"+keyword+"%");
			prsmt.setString(4, "%"+keyword+"%");
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderDetail od = new OrderDetail();
				od.setOrderNo(re.getString(1));
				od.setProductNo(re.getString(2));
				od.setQty(re.getInt(3));
				od.setOrderPrice(re.getDouble(4));
				list.add(od);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	

}
